﻿using System;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace VsTortoiseSVN
{
    /// <summary>
    /// Represents the VsTortoise diagnostics output pane.
    /// Pane is created when enabled and never removed again in the Visual Studio session where it has been enabled.
    /// </summary>
    public class Output
    {
        private OutputWindowPane _outputPane;
        private OutputWindowPanes _outputPanes;
        private bool _enabled;

        #region Singleton
        private static Output _instance;
        public static Output Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Output();

                return _instance;
            }
        }
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        protected Output()
        {
            OutputWindow outputWindow = VsTortoiseSVN.Globals.DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object as OutputWindow;
            _outputPanes = outputWindow.OutputWindowPanes;
        }

        /// <summary>
        /// Gets/sets whether VsTortoise DebugOutput is enabled.
        /// </summary>
        public bool Enabled
        {
            get
            {
                return _enabled;
            }

            set
            {
                _enabled = value;
                if (!_enabled)
                    return; // we don't remove the existing pane

                // Pane exists already? Nothing further to do...
                if (IsPresent)
                    return;

                // Adds VsTortoise output pane in the moment it has been enabled.
                _outputPane = _outputPanes.Add("VsTortoise");
                if (_outputPane != null)
                {
                    _outputPane.Activate();
                    OutputString("Hello, I'm the VsTortoise diagnostics output pane.\n");
                    OutputString("You can deactivate me in Mainmenu -> Tools -> Options -> VsTortoise.\n");
                }
            }
        }

        /// <summary>
        /// Gets whether the VsTortoise DebugOutput pane is present.
        /// </summary>
        public bool IsPresent
        {
            get
            {
                if (_outputPane != null)
                {
                    foreach (OutputWindowPane pane in _outputPanes)
                    {
                        if (pane.Guid.Equals(_outputPane.Guid))
                            return true;
                    }
                }

                return false;
            }
        }

        /// <summary>
        /// Clears the VsTortoise DebugOutput pane.
        /// </summary>
        public void Clear()
        {
            if (_outputPane != null)
                _outputPane.Clear();
        }

        /// <summary>
        /// Appends a copy of the specified string to the VsTortoise output pane. 
        /// </summary>
        public void Write(string text)
        {
            OutputString(text);
        }

        /// <summary>
        /// Appends a copy of the specified string and the default line terminator to the VsTortoise output pane. 
        /// </summary>
        public void WriteLine(string text)
        {
            OutputString(text + Environment.NewLine);
        }

        /// <summary>
        /// Outputs a string to the VsTortoise DebugOutput pane.
        /// </summary>
        private void OutputString(string text)
        {
            if (Enabled)
            {
                System.Diagnostics.Debug.Write(text);

                if (_outputPane != null)
                    _outputPane.OutputString(text);
            }
        }
    }
}
